<?
error_reporting(E_ALL ^ E_NOTICE);
define('ROOT_PATH', '/var/www/html/new/d/');
include("/var/www/html/new/com.inc.php");
include(ROOT_PATH . "common/errBack.php");
include(ROOT_PATH . "common/func.inc.php");
include(ROOT_PATH . "common/err_code.php");
include(ROOT_PATH . "class/ProductInfo.php");
include(ROOT_PATH . "class/Member.php");
include(ROOT_PATH . "class/DisOrder.php");
include(ROOT_PATH . "class/Sale.class.php");
include(ROOT_PATH . "class/SoapInit.class.php");
include(ROOT_PATH . "class/MemberAccount.class.php");
$soap = SoapInit::GetSoapInside();
$memberID = $_SESSION['sid'];
// print_r($_REQUEST);
// print_r($_SESSION);
// exit;
if(!$pid=abs($_REQUEST['pid'])){ getOut("index.html");}
$aid=abs($_REQUEST['aid']);
if(p_match($begintime=$_REQUEST['begintime']))  exit("游玩时间含有非法字符");
if(p_match($ordername=$_REQUEST['ordername']))  exit("取票人含有非法字符");
if(p_match($ordertel=$_REQUEST['ordertel']))  exit("取票人电话含有非法字符");
if(!$tnum=abs($_REQUEST['tnum']))  exit("门票数不能为零");
if($remoteorder=trim($_REQUEST['remoteorder'])){
	if(p_match($remoteorder)||strlen($remoteorder)>256) exit("远端订单号非法");
}
$paymode=abs($_REQUEST['paymode']);
$totalTnum=$tnum;
foreach((array)$_REQUEST['c_pids'] as $c_pid=>$c_num){
	$totalTnum+=$c_num;
}

//if ($_REQUEST['c_pids']) print_r($_REQUEST);
for($n=1;$n<=$totalTnum;$n++){
	$tourists = array();
	$idcards  = array();
	if($_REQUEST['tourist_name_'.$n]||$_REQUEST['tourist_cert_'.$n]){
		$tourists[]= mysql_real_escape_string($_REQUEST['tourist_name_'.$n]);
		$idcards[] = mysql_real_escape_string($_REQUEST['tourist_cert_'.$n]);
	}
}
$option=array();
$option["pid"]=$pid;
$option["begintime"] = $begintime;
$option["ordername"] = trim($ordername);
$option["ordertel"]  = $ordertel;
$option["tnum"]      = $tnum;
$option["remoteorder"]=$remoteorder;
$option["c_pids"]=$_REQUEST['c_pids'];

$option["from"]=$_REQUEST['from'];
$option['memo']=mysql_real_escape_string($_REQUEST['memo']);
$option['origin']=$_REQUEST['origin'];
if(isset($_REQUEST['sfz'])){$option['sfz']=$_REQUEST['sfz'];}
$option['tourists']=$tourists;
$option['idcards'] =$idcards;
$option['paymode'] =$paymode;


if ( !$_SESSION['memberID']) {
    if ($option['paymode']!=1 || $option['paymode']!=4) {
//        ErrorResponse('产品支付方式错误，只能为在线支付或现场支付');
    }
    $identifyFlag = false;
    //TODO::散客注册
    $memObj   = new \pft\Member\MemberAccount($GLOBALS['le']);
    $memberID = $memObj->mobileRegister($option['ordertel'], 5, '','',$_SESSION['apply_did']);
}
else {
    if ($_SESSION['dtype']==5) {
        $identifyFlag = false;
    } else {
        $identifyFlag = true;
    }
    $memberID = $_SESSION['memberID'];

}
//var_dump($identifyFlag);exit;
//print_r($_SESSION);
$pro        = new ProductInfo($soap, $pid, $aid);
$mem        = new Member($soap, $_SESSION['apply_did']);
//print_r($_SESSION);
//exit;
if ($identifyFlag) {
    $orderObj   = new DisOrder($soap,$pro,$mem);
} else {
    $orderObj   = new SaleOrder($soap,$pro,$mem);
}

try{
//	print_r($option);
	$r = $orderObj->order($option, $aid);
	$url="/new/d/orderSuc.html?ordernum=".$r['orderNum'] . '&ot=' . md5($r['orderType']) ;
}catch(DisOrderException $e){
	getOut("/new/d/plist.html",$e->getMessage());
}catch(InterFaceException $e){
	$url="/new/d/orderFailure.html?msg=".$e->getMessage()."：".$SoapRequestChk[$e->getCode()]."(".$e->getCode().")";
}catch(MsgException $e){
	$url="/new/d/orderFailure.html?msg=".$e->getMessage()."：".telcode($e->getCode())."(".$e->getCode().")";
}
if($_REQUEST['saveTo']==1){//常用联系人存储
	$m->addCinfo($ordername,$ordertel);
}
header("Location:$url");
function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}
?>